/**
 * 
 */
package co.com.ebusiness.trader.pages;

import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Session;
import org.got5.tapestry5.jquery.ImportJQueryUI;

import co.com.ebusiness.trader.core.model.Empresa;
import co.com.ebusiness.trader.seguridad.model.UsuarioEmpresa;
import co.com.ebusiness.trader.services.TraderService;

/**
 * @author Luis Agudelo
 *
 */
@Import(stylesheet = { "context:css/jquery/jquerydatatables.css" })
//Apply the "smoothness" theme (downloaded from http://jqueryui.com/themeroller/)
@ImportJQueryUI(theme = "context:css/jquery/themes/smoothness/jquery-ui-1.8.23.custom.css")
public class Consignacion {
	
	static private final int MAX_RESULTS = 200;

	// Screen fields

	@Property
	@SuppressWarnings("unused")
	private List<co.com.ebusiness.trader.model.Consignacion> consignaciones;

	@Property
	@SuppressWarnings("unused")
	private co.com.ebusiness.trader.model.Consignacion consignacion;

	@Property
	@Persist(PersistenceConstants.FLASH)
	@SuppressWarnings("unused")
	private String errorMessage;

	// Generally useful bits and pieces

	@Inject
	private TraderService traderService;
	
	@Inject
 	private Request request;

	// The code

	// setupRender() is called by Tapestry right before it starts rendering the page.

	void setupRender() {
		Session session = request.getSession(false);
		Empresa empresa = null;
		if (session != null && session.getAttribute("state:Index::empresa") != null){
			empresa = ((UsuarioEmpresa)session.getAttribute("state:Index::empresa")).getEmpresa();
 		}
		consignaciones = traderService.getAllConsignaciones(empresa);
	}
	
	public JSONObject getOptions() {
		
		// The available options are documented at http://www.datatables.net/ref
		
		JSONObject options = new JSONObject();
		options.put("bJQueryUI", "true");
		return options;
	}

	// Handle event "delete"
	@CommitAfter
	void onDelete(co.com.ebusiness.trader.model.Consignacion consignacion) {
		
		try {
			traderService.eliminarConsignacion(consignacion);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}
